package com.my.code.map;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 使用LinkedHashMap实现LRU Cache
 *
 * @author zzl
 * @date 2020/10/12
 */
public class LRUCache<K, V> extends LinkedHashMap<K, V> {

    public final int CACHE_SIZE;

    public LRUCache(int cacheSize) {
        super((int) (cacheSize / 0.75f) + 1, 0.75f, true);
        this.CACHE_SIZE = cacheSize;
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
        return size() > CACHE_SIZE;
    }
}
